package com.gxa.meiye.admin.controller;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.meiye.admin.service.OrdersService;
import com.gxa.meiye.admin.utils.TokenUtil;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.exception.BusinessException;
import com.gxa.meiye.core.exception.ExceptionCode;
import com.gxa.meiye.core.pojo.domain.OrderInfo;
import com.gxa.meiye.core.pojo.domain.OrderdetailedInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @date 2022/7/19 10:05
 */
@Api(tags = "订单管理模块")
@RestController
@RequestMapping("/mlsorder")
public class OrdersController {

    @Autowired
    private OrdersService orderService;

    @Autowired
    private TokenUtil tokenUtil;

    @ApiOperation("查询未接订单")
    @ResponseBody
    @GetMapping("getByTheraist")
    public JsonResult getByTheraist(ServletRequest request) throws JsonProcessingException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String token = httpRequest.getHeader("token");
        Map user = tokenUtil.getUserByToken(token);
        Integer id = (Integer)user.get("id");
        JsonResult jsonResult = new JsonResult();
        List<OrderInfo> orders = this.orderService.findByTherapistId(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(orders);
        if (orders.size()==0){
            jsonResult.setCode("201");
            jsonResult.setMsg("没有订单");
        }
        return jsonResult;
    }

    @ApiOperation("同意未接订单")
    @PostMapping("/agree")
    public JsonResult agreeOrder(@ApiParam(name = "id",value = "订单的id",type = "Integer")Integer id) throws BusinessException {
        if (id == null){
            throw new BusinessException(ExceptionCode.NULL_PARAMETER);
        }
        JsonResult jsonResult = new JsonResult();
        boolean updateStatusById = this.orderService.updateStatusById(id,4);
        if (updateStatusById){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改订单状态成功，已经成功接单");
        }else {
            jsonResult.setCode("201");
            jsonResult.setMsg("接单失败，无法更改订单状态");
        }
        return jsonResult;
    }

    @ApiOperation("联系订单客户")
    @GetMapping("/contact")
    public JsonResult contactConsumer(Integer id) throws BusinessException {
        if (id == null){
            throw new BusinessException(ExceptionCode.NULL_PARAMETER);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        String phone = this.orderService.getConsumerPhone(id);
        jsonResult.setData(phone);
        if (phone==null){
            jsonResult.setMsg("无法查到该用户手机号码");
        }
        return jsonResult;
    }

    @ApiOperation("查看我接的订单")
    @ResponseBody
    @GetMapping("/getMyOrder")
    public JsonResult getMyOrder(ServletRequest request) throws JsonProcessingException {
        JsonResult jsonResult = new JsonResult();
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String token = httpRequest.getHeader("token");
        Map user = tokenUtil.getUserByToken(token);
        Integer id = (Integer)user.get("id");
        List<OrderInfo> orders = this.orderService.myOrder(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(orders);
        if (orders.size()==0){
            jsonResult.setCode("200");
            jsonResult.setMsg("没有订单，请接单以后再来查看");
        }
        return jsonResult;
    }


    @ApiOperation("查询订单的详细消息")
    @ResponseBody
    @GetMapping("/getOrderInfo")
    public JsonResult getOrderInfo(@ApiParam(name = "id",value = "订单的id",type = "Integer")Integer id) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (id == null){
            throw new BusinessException(ExceptionCode.NULL_PARAMETER);
        }
        OrderdetailedInfo info = this.orderService.getDetailedInfo(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(info);
        return jsonResult;
    }

    @ApiOperation("完成服务")
    @ResponseBody
    @GetMapping("/finishOrder")
    public JsonResult finishOrder(@ApiParam(name = "id",value = "订单的id",type = "Integer")Integer id) throws BusinessException {
        if (id == null){
            throw new BusinessException(ExceptionCode.NULL_PARAMETER);
        }
        JsonResult jsonResult = new JsonResult();
        boolean updateStatusById = this.orderService.updateStatusById(id,5);
        if (updateStatusById){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改订单状态成功，已经成功服务");
        }else {
            jsonResult.setCode("201");
            jsonResult.setMsg("接单失败，无法更改订单状态");
        }
        return jsonResult;
    }


}
